package com.supply.dl.activity.seachmode;

import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

import com.supply.dl.R;
import com.supply.dl.activity.BaseActivity;
import com.supply.dl.activity.MoveHouseSendGoodsDetailActivity;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.InAndOutBoundLocationBean;
import com.supply.dl.bean.OrderInAndOutBoundBean;
import com.supply.dl.bean.request.OrderBoundRequest;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.DateUtils;
import com.supply.dl.utils.StringUtils;
import com.supply.dl.utils.utils.ToastUtil;

import java.util.ArrayList;

import android.support.v4.util.ArrayMap;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 移库发货
 * <p>
 * Author:  tian <br/>
 * Time:  2018/7/4 0:25
 */
public class MoveHouseGoodsModle extends ISearchModle<OrderInAndOutBoundBean> {

    @Override
    public void setSearchAdapter() {
        if (adapter != null)
            return;


        resource = new ArrayList<>();
        adapter = new NormalAdapter<OrderInAndOutBoundBean>(context, resource) {
            @Override
            protected void convert(ViewHolder holder, OrderInAndOutBoundBean data) {
                holder.setText(R.id.order_id_tv, "移库单号" + data.orderNo);
                holder.setText(R.id.status_tv, data.displayOrderStatusName);
                holder.setText(R.id.order_type_tv, "移库单类型:" + data.orderTypeName);
                holder.setText(R.id.out_ck_tv, "出库仓库:" + data.outWhName);
                holder.setText(R.id.in_ck_tv, "入库仓库:" + data.inWhName);
                holder.setText(R.id.time_tv, StringUtils.isEmpty(String.valueOf(data.addTime)) ? ""
                        : DateUtils.getSimpleDateTimeByMill(String.valueOf(data.addTime), DateUtils.DATE_TIME_FORMAT));//建单时间
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.item_move_house_send_goods_orders;
            }
        };

        adapter.setOnItemClickListener(new NormalAdapter.OnItemClickListener<OrderInAndOutBoundBean>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, OrderInAndOutBoundBean data, int position) {

                OrderInAndOutBoundBean orderInAndOutBoundBean = new OrderInAndOutBoundBean();
                orderInAndOutBoundBean.orderId = data.orderId;
                orderInAndOutBoundBean.orderNo = data.orderNo;
                orderInAndOutBoundBean.noticeOrderId = data.noticeOrderId;
                orderInAndOutBoundBean.noticeOrderNo = data.noticeOrderNo;
                orderInAndOutBoundBean.displayOrderStatusName = data.displayOrderStatusName;
                orderInAndOutBoundBean.orderTypeName = data.orderTypeName;
                Bundle b = new Bundle();
                b.putSerializable("OrderInAndOutBoundBean",orderInAndOutBoundBean);
                BaseActivity ac = (BaseActivity) context;
                ac.jumpActivity(MoveHouseSendGoodsDetailActivity.class,b);
            }
        });
    }

    @Override
    public void setHintText() {
        hintText = "移库单号";
    }

    @Override
    public void setTitleShow() {
        titleShow = "移库发货";
    }

    @Override
    public void init(Context context) {
        this.context = context;
        setTitleShow();
        setSearchAdapter();
    }

    @Override
    public void vistData(String keyWord, ArrayMap<String, String> filterMap, OnVistDataSuccess onVistDataSuccess) {


        Map<String,String> map = new HashMap<>();
        map.put("key",keyWord);//测试单号 TW1804170248
        if (filterMap != null) {
            if(filterMap.containsKey(AppConfig.SearchFilterAction.MOVE_HOURSE_ORDER_TYPE))
                map.put("orderType",filterMap.get(AppConfig.SearchFilterAction.MOVE_HOURSE_ORDER_TYPE));
            if(filterMap.containsKey(AppConfig.SearchFilterAction.MOVE_HOUSER_ORDER_STATUS))
                map.put("orderStatus",filterMap.get(AppConfig.SearchFilterAction.MOVE_HOUSER_ORDER_STATUS));
            if (filterMap.containsKey(AppConfig.SearchFilterAction.START_TIME))
                map.put("createTimeStart",filterMap.get(AppConfig.SearchFilterAction.START_TIME));
            if (filterMap.containsKey(AppConfig.SearchFilterAction.END_TIME))
                map.put("createTimeEnd",filterMap.get(AppConfig.SearchFilterAction.END_TIME));
        } else {
            map.put("orderType","");
            map.put("orderStatus","");
            map.put("createTimeStart","");
            map.put("createTimeEnd","");
        }


        //开始进行网络的访问
        ApiService mApiService = HttpMethod.getInstance().create(ApiService.class);
        mApiService.moveHouseSendGoodsOrderList(map, String.valueOf(pageNum), HttpManger.PAGE_SIZE)
                .compose(RxSchedulers.<BaseInfo<List<OrderInAndOutBoundBean>>>compose(context))
                .subscribe(new BaseObserver<List<OrderInAndOutBoundBean>>() {
                    @Override
                    public void onSuccess(BaseInfo<List<OrderInAndOutBoundBean>> baseInfo) {
                        if (baseInfo != null) {
                            if (baseInfo.getData() != null) {
                                if(pageNum == 1){
                                    resource.clear();
                                }
                                if(baseInfo.getData().size() < 10){
                                    hasMoreData = false;
                                }
                                resource.addAll(baseInfo.getData());
                                adapter.notifyDataSetChanged();
                            }
                        }
                        if(resultListener != null)
                            resultListener.onResult();
                    }

                    @Override
                    public void onFailure(String msg) {
                        ToastUtil.showToast(context,msg);
                        if(resultListener != null)
                            resultListener.onResult();
                    }
                });
    }

}











